package
{
	import flash.utils.Dictionary;
	
	import org.flixel.*;

	public class PlayerAchievementComponent extends FlxGroup
	{
		protected static const ACHIEV_BAR_WIDTH:int = 120;
		protected static const ACHIEV_BAR_HEIGHT:int = 400;
		protected static const ACHIEV_BAR_X:int = 0;
		protected static const ACHIEV_BAR_Y:int = 94;
		
		protected static const ACHIEV_POPUP_WIDTH:int = 400;
		protected static const ACHIEV_POPUP_HEIGHT:int = 120;
		
		protected var up:FlxSprite;
		protected var down:FlxSprite;
		protected var lockUp:Boolean;
		protected var lockDown:Boolean;
		
		protected var pages:Array;
		protected var curPage:int;
		protected var totalPages:int;
		protected var iconToText:Dictionary;
		
		protected var currentPopup:FlxGroup;
		protected var popupID:int;
		protected var handledOverlap:Boolean;
		protected var stillOverlapping:Boolean;
		protected var overlapTimer:Number;
		
		protected var startX:int;
		protected var startY:int;
		
		protected var popupX:int;
		protected var popupY:int;
		
		protected var mouse:FlxSprite;
		protected var friendStats:Boolean;
		protected var otherAS:AchievementSystem
		
		public function PlayerAchievementComponent(x:int = ACHIEV_BAR_X, y:int=ACHIEV_BAR_Y, friendStats:Boolean = false, otherAS:AchievementSystem = null)
		{
			startX = x;
			startY = y;
			
			popupX = startX + ACHIEV_BAR_WIDTH;
			popupY = startY;
			
			this.friendStats = friendStats;
			this.otherAS = otherAS;
			
			this.add(new FlxSprite(startX, startY, Res.ChallengeBar));
			
			mouse = new FlxSprite(FlxG.mouse.screenX, FlxG.mouse.screenY, Res.blankImage);
			
			init();
		}
		
		
		private function init():void {
			var rows:int = 3;
			var numAchievs:int = 0;
			if (friendStats) {
				numAchievs = otherAS.numberOfAchievements();
			} else {
				numAchievs = Elemental.achievements.numberOfAchievements();
			}
			var numPages:int = numAchievs / rows + Math.min(1, numAchievs % rows);
			
			pages = new Array();
			iconToText = new Dictionary();
			currentPopup = new FlxGroup();
			handledOverlap = true;
			popupID = -1;
			stillOverlapping = false;
			overlapTimer = 0;
			
			for (var i:int = 0; i < numPages; i++) {
				var page:FlxGroup = new FlxGroup;
				for (var j:int = 0; i*rows + j < numAchievs && j < rows; j++) {
					var a:AchievementInfo = null;
					if (friendStats) {
						a = otherAS.getAchievement(i*rows+j);
					} else {
						a = Elemental.achievements.getAchievement(i*rows+j);
					}
					var s:FlxSprite = new FlxSprite(startX + 30, startY + 90 + j*90,a.getIcon());
					iconToText[s] = a.getText();
					page.add(s);
					if (!a.complete()) {
						var num:FlxText = new FlxText(startX + 30, startY + 90 + j*90, 80, (i*rows + j + 1) + "", false);
						num.setFormat("Courier New", 28, 0xFFFFFF, "center", 0, true);
						num.y =  startY + 90 + j*90 + (80 - num.height)/2;
						page.add(num);
					}
				}
				pages.push(page);
			}
			
			if (numPages > 1) {
				totalPages = numPages;
				
				up = new FlxSprite(startX + 20, startY + 40, Res.upScrollArrow);
				down = new FlxSprite(startX + 20, startY + ACHIEV_BAR_HEIGHT - 25, Res.upScrollArrow);
				down.angle = 180;
				this.add(down);
			}
			curPage = 0;
			totalPages = numPages;
			this.add(pages[curPage]);
		}
		
		public override function update():void {
			super.update();
			
			mouse.x = FlxG.mouse.screenX;
			mouse.y = FlxG.mouse.screenY;
			
			FlxG.overlap(mouse, pages[curPage], displayPopup);
			
			if (FlxG.mouse.justPressed()) {
				checkPageChange();
			}
			
			if (stillOverlapping) {
				overlapTimer += FlxG.elapsed;
				if (overlapTimer > .1) {
					stillOverlapping = false;
					popupID = -1
				}
			}
		}
		
		private function checkPageChange():void {
			if (!lockUp && up.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)) {
				this.remove(pages[curPage], true);
				curPage--;
				this.add(pages[curPage]);
				popupID = -1;
				if (curPage == 0) {
					lockUp = true;
					this.remove(up, true);
				}
				if (curPage == totalPages - 2) {
					lockDown = false;
					this.add(down);
				}
			} else if (!lockDown && down.overlapsPoint(FlxG.mouse.screenX, FlxG.mouse.screenY)) {
				this.remove(pages[curPage], true);
				curPage++;
				this.add(pages[curPage]);
				popupID = -1;
				if (curPage == 1) {
					lockUp = false;
					this.add(up);
				}
				if (curPage == totalPages - 1) {
					lockDown = true;
					this.remove(down, true);
				}
			}
		}
		
		private function displayPopup(obj1:FlxObject, obj2:FlxObject):void {
			var icon:FlxSprite = obj2 as FlxSprite;
			stillOverlapping = true;
			overlapTimer = 0;
			if (popupID != icon.y) {
				handledOverlap = false;
				popupID = icon.y;
				
				var popup:FlxGroup = new FlxGroup();
				var b:FlxSprite = new FlxSprite(popupX - 25, icon.y, Res.achievPopUpBG);
				popup.add(b);
				
				var txt:FlxText = new FlxText(popupX + 65, icon.y + 20, ACHIEV_POPUP_WIDTH - 115, iconToText[icon], false);
				txt.setFormat("Courier New", 18, 0x000000,  "center");
				txt.y = b.y + (b.height - txt.height)/2;  
				popup.add(txt);
				
				currentPopup = popup;
			}
		}
		
		public function isStillOverlapping():Boolean {
			return stillOverlapping;
		}
		
		public function hasPopup():Boolean {
			return !handledOverlap;
		}
		
		public function getNextPopup():FlxGroup {
			handledOverlap = true;
			return currentPopup;
		}
	}
}